package com.stylefeng.guns.modular.stockmanagement.service.impl;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.dto.GoodsOverflowAddRequest;
import com.stylefeng.guns.modular.manager.service.IManagerGoodsService;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsOverflowListService;
import com.stylefeng.guns.modular.system.model.GoodsOverflow;
import com.stylefeng.guns.modular.system.dao.GoodsOverflowMapper;
import com.stylefeng.guns.modular.stockmanagement.service.IGoodsOverflowService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.system.model.GoodsOverflowList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
public class GoodsOverflowServiceImpl extends ServiceImpl<GoodsOverflowMapper, GoodsOverflow> implements IGoodsOverflowService {

    @Autowired
    private IGoodsOverflowListService goodsOverflowListService;
    @Autowired
    private IManagerGoodsService managerGoodsService;

    @Override
    public boolean insertOverflowAndGoods(GoodsOverflowAddRequest request) {
        GoodsOverflow goodsOverflow = new GoodsOverflow();
        BeanUtils.copyProperties(request, goodsOverflow);
        boolean success = this.insert(goodsOverflow);
        List<GoodsOverflowList> goodsOverflowList = request.getGoodsOverflowList();
        goodsOverflowList.forEach(goodsOverflowListEntity -> goodsOverflowListEntity.setGoodsOverflowId(goodsOverflow.getId()));
        boolean insertOverflowGoodsSuccess = goodsOverflowListService.insertBatch(goodsOverflowList);
        return success && insertOverflowGoodsSuccess;
    }

    @Override
    public Page<GoodsOverflow> selectPageWithDataScope(Page<GoodsOverflow> page, Wrapper<GoodsOverflow> wrapper) {
        if(ShiroKit.isAdmin()){
            Page<GoodsOverflow> result = this.selectPage(page, wrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            List<GoodsOverflow> result = this.baseMapper.selectPageWithDataScope(page,dataScope,wrapper.getEntity());
            page.setRecords(result);
            return page;
        }
    }
}
